<?php
namespace Hawk\Controller;
use \Hawk\Controller\IndexController;
class ServiceController extends IndexController 
{
	/**
     * 客户列表
     * @author hawk <2271030162@qq.com>
     * @return mix
     */
	public function lst(){
		$model = M('');
		if(IS_POST){
			if(!empty($_POST['part_id'])){
				$pid = $_POST['part_id'];
				$search['part_id'] = $_POST['part_id'];
			}else{
				$pid = '';
			}
			if(!empty($_POST['sale_worker'])){
				$sale_worker = $_POST['sale_worker'];
				$search['sale_worker'] = $_POST['sale_worker'];
			}else{
				$sale_worker = '';
			}
			if(!empty($_POST['cus_id'])){
				$customer = $_POST['cus_id'];
				$search['cus_id'] = $_POST['cus_id'];
			}else{
				$customer = '';
			}
			if(!empty($_POST['time_start'])){
				$time_start =$_POST['time_start'];
				$search['time_start']=$_POST['time_start'];
			}else{
				$time_start ='';
			}
			if(!empty($_POST['time_end'])){
				$time_end =$_POST['time_end'];
				$search['time_end']=$_POST['time_end'];
			}else{
				$time_end = '';
			}
		}
		if(!empty($pid)){
			$part_id = $this->_part_id($pid);
			$sale_admin = $model
			->table(C('DB_PREFIX').'admin')
			->field('id,nickname')
			->where(array('part_id'=>array('IN',$part_id)))
			->select();
			foreach ($sale_admin as $k => $v) {
				$sale_admin_id[$k]=$v['id'];
			}
			if(!empty($sale_admin_id)){
				$where['sale_worker']=array('IN',$sale_admin_id);
			}else{
				$where['sale_worker']=array('eq',0);
			}
		}
		if(!empty($sale_worker)){
			$where['sale_worker']=$sale_worker;
		}
		if(!empty($customer)){
			$where['id']= $customer;
		}
		if(!empty($time_start)&&!empty($time_end)){
			$where['addtime']=array(array('gt',strtotime($time_start)),array('lt',strtotime($time_end)+3600*24)); 
		}else if(!empty($time_start)&&empty($time_end)){
			$where['addtime']=array('gt',strtotime($time_start));
		}else if(empty($time_start)&&!empty($time_end)){
			$where['addtime']=array('lt',strtotime($time_end)+3600*24);
		}
		$where['is_checked']=array('gt',5);
		$server_id = $this->_server_id(session('staff_info')['id']);
		$where['server_worker']= array('0'=>'IN','1'=>$server_id);		
		$count = $model
		->table(C('DB_PREFIX').'customer')
		->where($where)
		->count();
		$page = new \Think\Page($count, 10);
		// 配置翻页的样式
		$page->setConfig('prev', '上一页');
		$page->setConfig('next', '下一页');
		$list['page'] = $page->show();
		$list['list'] = $model
		->table(C('DB_PREFIX').'customer')
		->where($where)
		->limit($page->firstRow . ',' . $page->listRows)
		->select();
		$amodel=D('Hawk/Admin');
		foreach ($list['list'] as $k => $v) {
			$list['list'][$k]['server_worker_name']=$amodel->getAdminName($v['server_worker']);
			$list['list'][$k]['sale_worker_name']=$amodel->getAdminName($v['sale_worker']);
			$list['list'][$k]['sale_head_name']=$amodel->getAdminName($v['sale_head']);
			$list['list'][$k]['sale_manager_name']=$amodel->getAdminName($v['sale_manager']);
		}
		//查询部门
		$list['part'] =$model->table(C('DB_PREFIX').'part')->select();
		//查询涉及业务员		
		$list['sale_worker'] = $model->table(C('DB_PREFIX').'admin')->order('id desc')->select();
		//查询涉及客户
		$list['customer'] = $model->table(C('DB_PREFIX').'customer')->select();
		$list['search'] = $search;
		$this->assign('list',$list);
		$tmodel = M('CustomerType');
		$type = $tmodel->select();
		$this->assign('type',$type);
	    $this->display(); 
	}
	/**
     * 涉及客服人员ID
     * @author hawk <2271030162@qq.com>
     * @param $role_id array
     * @return array
     */
	private function _server_id($role_id){
		$model=M('');
		$array_role_id = $this->_role_id($role_id);
		$array_admin_id = $model
		->table(C('DB_PREFIX').'admin_role')
		->field('admin_id')
		->where(array('role_id'=>array('IN',$array_role_id)))
		->select();
		if(!empty($array_admin_id)){
			foreach ($array_admin_id as $k => $v) {
				$server_id[] = $v['admin_id'];
			}
			return $server_id;
		}else{
			return null;
		}

	}
	/**
     * 部门层级筛选(返回部门ID)
     * @author hawk <2271030162@qq.com>
     * @param $pid int
     * @return array
     */
	private function _part_id($pid){
		$amodel = D('Hawk/Admin');
		$pmodel=M('Part');
		//通过部门层级查询
		if(empty($pid)){
			$pid = session('staff_info')['part_id'];
		}
		$part_id = $amodel->getChildren($pmodel,$pid);
		array_unshift($part_id, $pid);
		return $part_id;
	}
	/**
     * 角色层级筛选(返回角色ID)
     * @author hawk <2271030162@qq.com>
     * @param $id int
     * @return array
     */	
	private function _role_id($id){
		$amodel = D('Hawk/Admin');
		$almodel = M('AdminRole');
		$rmodel = M('Role');
		//角色层级查询
		$role = $almodel
		->where(array(
			'admin_id'=>array('eq',$id)
			))
		->find();
		$role_id = $amodel->getChildren($rmodel,$role['role_id']);
		array_unshift($role_id, $role['role_id']);
		return $role_id;
	}
	/**
     * 标记客户黑名单
     * @author hawk <2271030162@qq.com>
     * @return json
     */
	public function defriend(){
		if(IS_POST){
			$model=M('Customer');
			if($_POST['status']==1){
				$status=0;
				$msg='洗白';
			}
			if($_POST['status']==0){
				$status=1;
				$msg='拉黑'; 
			}
			$re = $model->where(array(
            	'id' => array('eq', I('post.id')),
        	))->setField('status', $status);
        	if($re!==false){
        		$content = $msg."（".I('post.id')."）成功！";
	        	hawk_operation_log(session('id'),hawk_get_ip(),$content);
        		echo json_encode(array('code'=>1,'msg'=>$msg.'成功！'));
        	}else{
        		echo json_encode(array('code'=>1,'msg'=>$msg.'失败！'));
        	}
		}
	}
	/**
     * 客服部客户分配页
     * @author hawk <2271030162@qq.com>
     * @return mix
     */
	public function allotlist(){
		$model = M('Customer');
		if(IS_POST){
			if(!empty($_POST['part_id'])){
				$pid = $_POST['part_id'];
				$search['part_id'] = $_POST['part_id'];
			}else{
				$pid = '';
			}
			if(!empty($_POST['sale_worker'])){
				$sale_worker = $_POST['sale_worker'];
				$search['sale_worker'] = $_POST['sale_worker'];
			}else{
				$sale_worker = '';
			}
			if(!empty($_POST['cus_id'])){
				$customer = $_POST['cus_id'];
				$search['cus_id'] = $_POST['cus_id'];
			}else{
				$customer = '';
			}
			if(!empty($_POST['time_start'])){
				$time_start =$_POST['time_start'];
				$search['time_start']=$_POST['time_start'];
			}else{
				$time_start ='';
			}
			if(!empty($_POST['time_end'])){
				$time_end =$_POST['time_end'];
				$search['time_end']=$_POST['time_end'];
			}else{
				$time_end = '';
			}
		}
		if(!empty($pid)){
			$part_id = $this->_part_id($pid);
			$sale_admin = $model
			->table(C('DB_PREFIX').'admin')
			->field('id,nickname')
			->where(array('part_id'=>array('IN',$part_id)))
			->select();
			foreach ($sale_admin as $k => $v) {
				$sale_admin_id[$k]=$v['id'];
			}
			if(!empty($sale_admin_id)){
				$where['sale_worker']=array('IN',$sale_admin_id);
			}else{
				$where['sale_worker']=array('eq',0);
			}
		}
		if(!empty($sale_worker)){
			$where['sale_worker']=$sale_worker;
		}
		if(!empty($customer)){
			$where['id']= $customer;
		}
		if(!empty($time_start)&&!empty($time_end)){
			$where['addtime']=array(array('gt',strtotime($time_start)),array('lt',strtotime($time_end)+3600*24)); 
		}else if(!empty($time_start)&&empty($time_end)){
			$where['addtime']=array('gt',strtotime($time_start));
		}else if(empty($time_start)&&!empty($time_end)){
			$where['addtime']=array('lt',strtotime($time_end)+3600*24);
		}
		$where['is_checked']=array('gt',5);
		$count = $model->where($where)->count();
		$page = new \Think\Page($count, 10);
			// 配置翻页的样式
		$page->setConfig('prev', '上一页');
		$page->setConfig('next', '下一页');
		$list['page'] = $page->show();
		$list['list'] = $model
		->where($where)
		->limit($page->firstRow . ',' . $page->listRows)
		->select();
		$amodel=D('Hawk/Admin');
		foreach ($list['list'] as $k => $v) {
			$list['list'][$k]['server_worker_name']=$amodel->getAdminName($v['server_worker']);
			$list['list'][$k]['sale_worker_name']=$amodel->getAdminName($v['sale_worker']);
			$list['list'][$k]['sale_head_name']=$amodel->getAdminName($v['sale_head']);
			$list['list'][$k]['sale_manager_name']=$amodel->getAdminName($v['sale_manager']);
		}
			//查询部门
		$list['part'] =$model->table(C('DB_PREFIX').'part')->select();
			//查询涉及业务员		
		$list['sale_worker'] = $model->table(C('DB_PREFIX').'admin')->order('id desc')->select();
			//查询涉及客户
		$list['customer'] = $model->table(C('DB_PREFIX').'customer')->select();
		$list['search'] = $search;
		$this->assign('list',$list);
		$tmodel = M('CustomerType');
		$type = $tmodel->select();
		$this->assign('type',$type);
		$this->display();
	}
	/**
     * 分配客户
     * @author hawk <2271030162@qq.com>
     * @return mix
     */
	public function allot(){
		if(IS_POST){
			$id = explode(',', $_POST['id']);
			if(is_array($id)){
				for ($i=0; $i <count($id) ; $i++) { 
				 	$data['server_worker']=$_POST['businesser'];
				 	$rmodel = M('Role');
        			$role_self = $rmodel->field('r.*,ar.admin_id')->alias('r')->join('left join hawk_admin_role ar on r.id=ar.role_id')->where('role_name="客服经理"')->find();        			 
        			$amodel = M('Admin');
        			$admin_self = $amodel->find($role_self['admin_id']);        			
				 	$data['server_manager']=$admin_self['id'];
					$model = M('Customer');
					$re = $model->where(array('id'=>array('eq',$id[$i])))->data($data)->save();
				 }
				 if($re!==false){
				 		$content = "分配（".$id."）给".$_POST['businesser'];
	        			hawk_operation_log(session('id'),hawk_get_ip(),$content);
						echo json_encode(array('code'=>1,'msg'=>'操作成功！'));
					}else{
						echo json_encode(array('code'=>0,'msg'=>'操作失败！'));
					}
			}else{
				$data['server_worker']=$_POST['businesser'];
				$model = M('Customer');
				$re = $model->where(array('id'=>array('eq',$id)))->data($data)->save($data);
				if($re!==false){
					echo json_encode(array('code'=>1,'msg'=>$msg.'操作成功！'));
				}else{
					echo json_encode(array('code'=>0,'msg'=>$msg.'操作失败！'));
				}
			}
		}else{
			$id = I('get.id');
			$pmodel = M('Part');
			$part = $pmodel->where('name="客服部"')->find();
			$model=M('Admin');
			$list = $model->where(array('part_id'=>array('eq',$part['id']),'is_use'=>array('eq',1)))->select();
			$this->assign('list',$list);
			$this->assign('id',$id);
			$this->display();
		}
	}
	/**
     * 客服部客户详情
     * @author hawk <2271030162@qq.com>
     * @return mix
     */
	public function moreinfo(){
		if(!empty(I('get.id'))){
			$id = I('get.id');
			//查询客户基本信息
			$cmodel=M('Customer');
			$cus['info'] = $cmodel->find($id);
			//客户类型
	        $tmodel = M('CustomerType');
			$type = $tmodel->select();
			$this->assign('type',$type);
			//资源类型
			$rmodel = M('ResourceType');
			$cus['rtype'] = $rmodel->select();
			//查询客户持仓信息
			$pmodel=M('StockLog');
			$cus['position'] = $pmodel->where(array('cus_id'=>array('eq',$id),'position_num'=>array('gt',0)))->select();
			//获取持仓股票现价
			if(count($cus['position']>0)){
				foreach ($cus['position'] as $k => $v) {
					if(substr($v['stock_code'], 0,1)==6){
						$code = 'sh'.$v['stock_code'];
					}else{
						$code = 'sz'.$v['stock_code'];
					}
					$url = "http://hq.sinajs.cn/list=".$code;
					$json=explode(',',substr(hawk_stock_price($url), 21,-3));
					$cus['position'][$k]['price']=$json[4];
					$stock[]=array('name'=>$json[0],
							'price'=>$json[4]);
					$market_value []= $v['position_num']*$json[4];			
				}
			}
			//持仓市值
			if(isset($market_value)){
				$cus['market'] = array_sum($market_value);
			}else{
				$cus['market']='';
			}			
			//查询客户沟通记录
			$clmodel=M('CommunicateLog');
			$where = array('cus_id'=>array('eq',$id));
			$count = $clmodel->where($where)->count();
			$page = new \Think\Page($count, 1);
			// 配置翻页的样式
			$page->setConfig('prev', '上一页');
			$page->setConfig('next', '下一页');
			$cus['page'] = $page->show();
			$cus['communicate'] = $clmodel->where($where)
										->order('addtime desc')
										->limit($page->firstRow.','.$page->listRows)
										->select();
			$this->assign('cus',$cus);
			$this->display();
		}else{
			hecho('无此权限！');
		}
	}
	/**
     * ajax修改总资金量
     * @author hawk <2271030162@qq.com>
     * @return json
     */
	public function ajaxmoney(){
		$model=M('Customer');
		$re = $model->where(array('id'=>array('eq',$_POST['cus_id'])))->data(array('financing'=>$_POST['financing']))->save();
		if($re>0){
			$content ="修改客户总资金量为".$_POST['financing'];
	        hawk_operation_log(session('id'),hawk_get_ip(),$content);
			echo json_encode(array('code'=>1,'msg'=>'操作成功！'));
		}else if($re===false){
			echo json_encode(array('code'=>0,'msg'=>'操作失败！'));
		}else{
			echo json_encode(array('code'=>0,'msg'=>'未改变！'));
		}
	}
}